<template>
  <div class="model-container">
    <div class="listName">系统角色管理</div>
    <!--{{this.threeList}}-->
    <!--搜索-->
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>快捷搜索</span>
        <div class="btns-wrapper-right">
          <el-button v-if="!searchShow" icon="el-icon-arrow-down" @click="searchShow = true"/>
          <el-button v-else icon="el-icon-arrow-up" @click="searchShow = false"/>
        </div>
      </div>
      <el-collapse-transition>
        <div v-show="searchShow" class="cardbody">
          <el-form ref="seacherParam" :model="seacherParam">
            <el-row :gutter="10">
              <el-col :xl="5" :lg="6" :md="6" :sm="8" >
                <el-col :xl="7" :lg="7" :md="8" :sm="8" class="input-name">角色名称</el-col>
                <el-col :xl="17" :lg="17" :md="16" :sm="16">
                  <el-form-item prop="name">
                    <el-input v-model="seacherParam.name" size="small" placeholder="请输入角色名称"/>
                  </el-form-item>
                </el-col>
              </el-col>
            </el-row>
            <div class="cardfooter">
              <el-button type="primary" size="mini" plain @click="seachFun">查询</el-button>
              <el-button type="warning" size="mini" plain @click="resetFun">重置</el-button>
            </div>
          </el-form>
        </div>
      </el-collapse-transition>
    </el-card>
    <!--表格-->
    <el-card style="margin-top: 10px">
      <div slot="header" class="clearfix">
        <div class="btns-wrapper-left">
          <el-button type="primary" plain @click="add">新增</el-button>
          <el-button type="danger" plain @click="del(0)">删除</el-button>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table v-loading="!dataOption" :stripe="true" :data="dataOption" border empty-text="暂无数据" @selection-change="changeFun">

          <el-table-column align="center" type="selection" width="55"/>

          <el-table-column align="center" prop="name" label="角色名称"/>

          <el-table-column align="center" prop="createTime" label="创建时间"/>

          <el-table-column align="center" prop="updateTime" label="更新时间"/>

          <el-table-column align="center" label="角色状态">
            <template slot-scope="scope">{{ scope.row.status === 0 ? '可用' : '不可用' }}</template>
          </el-table-column>
         <!-- <el-table-column align="center" prop="fname" label="上级">
          </el-table-column>-->

          <el-table-column align="center" label="操作" width="200px">
            <template slot-scope="scope">

              <el-button title="编辑" type="primary" circle icon="el-icon-edit" class="table-icon" @click="edit(scope.row)"/>

              <el-button title="删除" type="warning" circle icon="el-icon-delete" class="table-icon" @click="del(scope.row)"/>

            </template>
          </el-table-column>

        </el-table>

        <!--分页-->
       <!-- <el-pagination
          :current-page="pageNo"
          :page-sizes="[10, 20, 30]"
          :page-size="pageSize"
          :total="rowCount"
          background
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>-->
      </div>
    </el-card>

    <!--弹框-->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogState"
      :close-on-click-modal="false"
      :before-close="handleClose"
      top="5vh"
      width="30%">
      <el-form ref="dialogBindData" :model="dialogBindData" :rules="rules" label-width="30%" status-icon class="demo-ruleForm">
        <div class="formBody">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="dialogBindData.name" size="small" type="text" placeholder="请输入角色名称"/>
          </el-form-item>

            <el-form-item label="上级" prop="fid">
              <el-select v-model="dataOption.fid" :disabled="disableIinput" size="small" placeholder="请选择">
                <el-option
                  v-for="item in dataOption"
                  :key="item.roleId"
                  :label="item.name"
                  :value="item.roleId"/>
              </el-select>
            </el-form-item>

          <el-form-item label="描述" prop="description">
            <el-input v-model="dialogBindData.description" autosize type="textarea" placeholder="描述"/>
          </el-form-item>
          <el-form-item label="是否可用" prop="status">
            <el-radio-group v-model="dialogBindData.status">
              <el-radio :label="0">是</el-radio>
              <el-radio :label="1">否</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="分配权限" class="three">
            <el-tree
              v-loading="!threeList"
              v-if="isRouterAlive"
              ref="tree"
              :default-expanded-keys="threeCheck"
              :data="threeList"
              :props="defaultProps"
              show-checkbox
              check-strictly
              node-key="permId"
              highlight-current/>
          </el-form-item>

        </div>
        <div class="btns-wrapper">
          <el-button size="small" @click="handleClose">取 消</el-button>
          <el-button :loading="buttonState" type="primary" size="small" @click="submmitFun">保 存</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import * as system from '../../api/system'
import * as login from "../../api/login";
export default {
  data() {
    return {
      // 页面搜索
      seacherParam: {
        name: ''
      },
      searchShow: false,
      pageNo: 1,
      pageSize: 10,
      rowCount: null, // 总条数
      // 弹框
      dialogType: 1, // 1--增加  2--编辑  3--查看
      dialogState: false,
      // 按钮状态
      buttonState: false,
      // 列表数据
      dataOption: null,
      datavalID: [],
      // 提交内容
      dialogBindData: {
        roleId: null,
        name: '',
        description: '',
        status: 0,
        menuIds: []
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      },

      // Three树
      isRouterAlive: true,
      threeList: [],
      threeCheck: [],
      defaultProps: {
        children: 'childList',
        label: 'menuName'
      }

    }
  },
  computed: {
    dialogTitle() {
      if (this.dialogType === 1) {
        return '新增角色管理'
      }
      if (this.dialogType === 2) {
        return '编辑角色管理'
      } else {
        return '查看'
      }
    }
  },
  beforeMount() {
    this.getList(),
    this.threeMenuList()
  },
  methods: {
    seachFun() {
      const param = {
        current: 1,
        size: this.pageSize,
        name: this.seacherParam.name
      }
      system.searchRoleList(param).then(res => {
        if (res.data.code !== 1) {
          this.$message.error(res.data.msg)
          return false
        }
        if (res.data.data === 'null' || res.data.data === null) {
          return false
        }
        // 初始话表格数据
        this.dataOption = res.data.data.records
        // 初始化分页数据
        this.rowCount = res.data.data.total
        this.pageSize = res.data.data.size
      })
    },
    getList() {
      const param = {
        current: this.pageNo,
        size: this.pageSize
      }
      system.getRoleList(param).then(res => {
        if (res.data.code !== 1) {
          this.$message.error(res.data.msg)
          return false
        }
        if (res.data.data === 'null' || res.data.data === null) {
          return false
        }
        // 初始话表格数据
        this.dataOption = res.data.data.records
        // 初始化分页数据
        this.rowCount = res.data.data.total
        this.pageSize = res.data.data.size
      })
    },
    // 新增
    add() {
      this.dialogType = 1
      this.dialogState = true
      this.isRouterAlive = true
    },
    // 编辑
    edit(data) {
      this.dialogType = 2
      this.dialogState = true
      this.isRouterAlive = true
      this.dialogBindData.roleId = data.roleId
      this.getThreeCheckeListFun()
      this.reBindData(data)
    },
    // 提交
    submmitFun() {
      this.dialogBindData.menuIds = this.$refs.tree.getCheckedKeys()
      const param = {
        name: this.dialogBindData.name,
        fid:this.dataOption.fid,
        description: this.dialogBindData.description,
        status: this.dialogBindData.status,
        menuIds: this.dialogBindData.menuIds
      }
      if (this.dialogType !== 1) {
        param.roleId = this.dialogBindData.roleId
      }
      this.$refs.dialogBindData.validate((valid) => {
        if (valid) {
          if (this.dialogType === 1) {
            system.addRoleList(param).then(res => {
              if (res.data.code !== 1) {
                this.$message({
                  message: res.data.msg,
                  type: 'warning'
                })
                this.buttonState = false
                return false
              }
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.buttonState = false
              this.getList()
              this.handleClose()
            })
          } else {
            system.editorRoleList(param).then(res => {
              if (res.data.code !== 1) {
                this.$message({
                  message: res.data.msg,
                  type: 'warning'
                })
                this.buttonState = false
                return false
              }
              this.$message({
                message: '编辑成功',
                type: 'success'
              })
              this.buttonState = false
              this.getList()
              this.handleClose()
            })
          }
        } else {
          return false
        }
      })
    },
    // 删除
    changeFun(data) {
      this.datavalID = data
    },
    del(data) {
      if (this.datavalID.length === 0 && data === 0) {
        this.$message({
          type: 'warning',
          message: '请选需要删除的角色'
        })
        return false
      }
      this.$confirm('确定要删角色吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delList(data)
      })
    },
    delList(data) {
      const classID = []
      if (data === 0) {
        for (const val of this.datavalID) {
          classID.push(val.roleId)
        }
      } else {
        classID.push(data.roleId)
      }
      system.delRoleList(classID).then(res => {
        if (res.data.code === 1) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getList()
          this.dialogState = false
        }
      })
    },
    // three所有菜单
    threeMenuList() {
      system.getThreeList().then(res => {
        this.threeList = res.data.data
      })
    },
    threeMenuListForUser(){
      system.getUserMenu().then(res => {
        this.threeList = res.data.data
      })
    },
    // 反显匹配菜单树
    getThreeCheckeListFun() {
      const param = {
        roleId: this.dialogBindData.roleId
      }
      system.getThreeCheckeList(param).then(res => {
        const dataArr = res.data.data
        for (const val of dataArr) {
          if (val.checked === true) {
            this.threeCheck.push(val.permId)
          }
          if (val.childList !== null) {
            for (const data of val.childList) {
              if (data.checked === true) {
                this.threeCheck.push(data.permId)
              }
            }
          }
        }
        this.$refs.tree.setCheckedKeys(this.threeCheck)
      })
    },
    // 反显
    reBindData(data) {
      for (const key in this.dialogBindData) {
        this.dialogBindData[key] = data[key]
      }
    },
    // 重置
    resetFun() {
      this.$refs.seacherParam.resetFields()
      this.pageNo = 1
      this.pageSize = 10
      this.getList()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pageNo = val
      this.getList()
    },
    handleClose() {
      for (const key in this.dialogBindData) {
        if (key === 'status') {
          this.dialogBindData[key] = 0
        } else if (key === 'menuIds') {
          this.dialogBindData[key] = []
        } else {
          this.dialogBindData[key] = ''
        }
      }
      this.dialogState = false
      this.isRouterAlive = false
      this.threeCheck = []
      this.$refs.dialogBindData.clearValidate()
    }
  }
}
</script>
<style type="text/scss" lang="scss" scoped>

</style>
